package com.zte.springboot_smbms.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zte.springboot_smbms.pojo.SmbmsUser;
import com.zte.springboot_smbms.service.UsersService;
import com.zte.springboot_smbms.utils.StringToDateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/userController/")
public class UserController {
    @Autowired
    private UsersService usersService;

    @RequestMapping("/")
    public String logins(Model model){
//        model.addAttribute("userList",usersService.findAllUsers());
        return "login";
    }


    @RequestMapping("/findAllUser")
    public String findAllUser(Model model){
        model.addAttribute("userList",usersService.findAllUsers());
        return "userlist";
    }

    @RequestMapping("/findUserByParam")
    public String findUserByParam(Model model,String queryname,Integer queryUserRole){
        model.addAttribute("userList",usersService.findUserByParam(queryname,queryUserRole));
        return "userlist";
    }

    @RequestMapping("login")
    public String login(String uCode, String uPwd, Model model){
        SmbmsUser user = usersService.login(uCode, uPwd);
       if (user != null){

            List<SmbmsUser> allUsers = usersService.findAllUsers();
            model.addAttribute("loginUser",user);
            model.addAttribute("userList",allUsers);
            return "userlist";
        } else {
            return "login";
        }
    }


    @RequestMapping("/delUserById")
    public String delUserById(Model model,Long id){
        boolean b = usersService.delUserById(id);
        if (b){
            return "forward:findAllUser";
        }
        return "userlist";
    }

    @RequestMapping("addUser")
    public String addUser(SmbmsUser user){
        /*System.out.println(birthDay);
        StringToDateConverter stringToDateConverter = new StringToDateConverter();
        Date convert = stringToDateConverter.convert(birthDay);
        user.setBirthday(convert);*/
        boolean b = usersService.addUser(user);
        if (b){
            return "forward:findAllUser";
        } else {
            return "forward:findAllUser";
        }
    }

    @RequestMapping("findUserByCode")
    @ResponseBody
    public String findUserByCode(@ModelAttribute("SmbmsUser") SmbmsUser user){
        SmbmsUser b = usersService.findUserByCode(user.getUsercode());
        if (b!=null){
            return "exist";
        } else {
            return "";
        }
    }

    @RequestMapping("html_useradd")
    public String html_useradd(){
        return "useradd";
    }

    @RequestMapping("findUserById")
    public String findUserById(Long id ,String method,Model model){
        List<SmbmsUser> allUsersById = usersService.findAllUsersById(id);
        if (allUsersById.size()!=0){
            model.addAttribute("user",allUsersById.get(0));
            if ("usermodify".equals(method)){
                return "usermodify";
            }
            if ("userview".equals(method)){
                return "userview";
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

    @RequestMapping("userModify")
    public String userModify(SmbmsUser user ,Model model,String birthDay){
        /*Date birthday = user.getBirthday();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(birthday);
        Date convert = new StringToDateConverter().convert(format);
        user.setBirthday(convert);*/
//        System.out.println(birthDay);
//        StringToDateConverter stringToDateConverter = new StringToDateConverter();
//        Date convert = stringToDateConverter.convert(birthDay);
//        user.setBirthday(convert);
        boolean b = usersService.updateUserById(user.getId(), user);
        if (b){
            return "forward:findAllUser";
        } else {
            return "forward:findUserById?id=" + user.getId();
        }
    }

    @RequestMapping("matchUserByCode")
    public @ResponseBody
    String matchUserByCode(String method ,@RequestParam("usercode") String usercode){
        if ("ucexist".equals(method)) {
            ObjectMapper objectMapper = new ObjectMapper();
            String resultString = "";
            SmbmsUser user = usersService.findUserByCode(usercode);
            System.out.println(user);
            if (user!=null){
                resultString = "true";
            }
            System.out.println("matchUserByCode+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            return resultString;
        }
        return null;
    }
}
